$(function () {
    var dateRangePicker = $('#daterangepicker');

    var initDateRangePicker = function () {
        var rangePast = {
            '今天': [moment(), moment()],
            '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '最近7天': [moment().subtract(6, 'days'), moment()],
            '最近30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上个月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        };

        dateRangePicker.daterangepicker({
            ranges: rangePast,
            startDate: moment(),
            endDate: moment(),
            dateLimit: {days: 90},
            applyClass: "btn-primary btn-block",
            cancelClass: "btn-block",
            format: "YYYY/MM/DD",
            locale: {
                applyLabel: '确定',
                clearLabel: "取消",
                fromLabel: '开始时间',
                toLabel: '结束时间',
                weekLabel: '周',
                customRangeLabel: '选择范围',
                daysOfWeek: "日_一_二_三_四_五_六".split("_"),
                monthNames: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
                firstDay: 0
            },
            opens: "left"
        });

        var formatDate = function (date) {
            return $.format.date(new Date(date), "yyyy/MM/dd");
        };

        var defaultDate = formatDate(moment()) + ' - ' + formatDate(moment());

        dateRangePicker.val(defaultDate);
        $(".daterangepicker .ranges ul li:last").hide();
        $(".daterangepicker .ranges .daterangepicker_start_input").remove();
        $(".daterangepicker .ranges .daterangepicker_end_input").remove();
        $(".daterangepicker .ranges .cancelBtn").remove();
    };

    var initTable = function () {
        var table = $('#transferRecordList').DataTable({
            "processing": true,
            "serverSide": true,
            //"iDeferLoading": 0,
            "ajax": {
                "url": "pnr/reconciliation/getTransferRecord",
                "data": function (d) {
                    var dateRange = dateRangePicker.val().split(" - ");
                    d.startDate = $.format.date(new Date(dateRange[0]), "yyyyMMdd");
                    d.endDate = $.format.date(new Date(dateRange[1]), "yyyyMMdd");
                    d.iDisplayLength = d.length;
                    d.iDisplayStart = d.start;
                    d.sEcho = d.draw;
                },
                "type": "GET"
            },
            "columns": [
                {"data": "ordId"},
                {"data": "pnrSeqId"},
                {"data": "usrCustId"},
                {"data": "transAmt"},
                {"data": "transStat"},
                {"data": "pnrDate"}
            ],
            "aoColumnDefs": [{
                    "targets": 3, //转账金额
                    "mRender": function (data, type, row) {
                        return '<span class="text-danger bold">' + data + '</span>';
                    }
                }, {
                    "targets": 4, //状态
                    "mRender": function (data, type, row) {
                        switch (data) {
                            case 'S':
                                return '<span class="badge badge-success">成功</span>';
                            case 'F':
                                return '<span class="badge badge-important">失败</span>';
                            case 'I':
                                return '<span class="badge badge-grey">初始</span>';
                            case 'P':
                                return '<span class="badge badge-info">部分成功</span>';
                            case 'H':
                                return '<span class="badge badge-warning">经办</span>';
                            case 'R':
                                return '<span class="badge badge-inverse">拒绝</span>';
                            case 'N':
                                return '<span class="badge badge-purple">开启</span>';
                            case 'C':
                                return '<span class="badge badge-light">关闭</span>';
                            case 'U':
                                return '<span class="badge badge-light">已解冻</span>';
                            case 'NOTEXIST':
                                return '<span class="badge badge-light">不存在</span>';
                            case 'NOTRANS':
                                return '<span class="badge badge-light">无交易</span>';
                            default:
                                return '<span class="badge badge-info">data</span>';
                        }
                    }
                }, {
                    "targets": 5, //汇付交易日期
                    "mRener": function (data, type, row) {
                        var date = data.replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3");
                        return date;
                    }
                }],
            "order": [5, "desc"],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ","
            },
            "iDisplayLength": 25,
            "lengthMenu": [
                [5, 10, 25, 50, -1],
                [5, 10, 25, 50, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"l<"pull-right"T>r>t<"dt-panelfooter clearfix"ip>',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            }
        });
        /**
         * 查询按钮响应事件
         */
        $('#queryTransferRecordBtn').on('click', function () {
            table.ajax.reload();
        });
    };
    initDateRangePicker();
    initTable();
});